1811D - Umka and a Long Flight - CodeForces Solution


constructive algorithms implementation math

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;

long long x, y;
int n;
long long f[100];
void prepare() {
    f[0] = 1;
    f[1] = 1;
    for(int i = 2; i <= 47; i++) {
        f[i] = f[i - 1] + f[i - 2];
    }
}

inline bool check(int m, long long a, long long b, long long _a, long long _b) {
    if(!(a <= x && x <= _a && b <= y && y <= _b))
        return 0;
    if(m == 1) {
        return ((x == a && y == b) || (x == _a && y == _b));
    }
    long long l;
    if((n - m) % 2 == 1) {
        l = f[m + 1] - f[m];
        if(x <= a + l - 1) {
            return check(m - 1, a, b, a + l - 1, _b);
        }
        if(x >= _a - l + 1) {
            return check(m - 1, _a - l + 1, b, _a, _b);
        }
    }
    else {
        l = f[m + 1] - f[m];
        if(y <= b + l - 1) {
            return check(m - 1, a, b, _a, b + l - 1);
        }
        if(y >= _b - l + 1) {
            return check(m - 1, a, _b - l + 1, _a, _b);
        }
    }
    return 0;
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    if(fopen("hth.inp", "r")) {
        freopen("hth.inp", "r", stdin);
        freopen("hth.out", "w", stdout);
    }
    int t;
    cin >> t;
    prepare();
    while(t --) {
        cin >> n >> x >> y;
        if(check(n, 1, 1, f[n], f[n + 1]))
            cout << "YES\n";
        else
            cout << "NO\n";
    }
}


Comments

Submit
0 Comments
More Questions

734A - Anton and Danik
1300B - Assigning to Classes
1647A - Madoka and Math Dad
710A - King Moves
1131A - Sea Battle
118A - String Task
236A - Boy or Girl
271A - Beautiful Year
520B - Two Buttons
231A - Team
479C - Exams
1030A - In Search of an Easy Problem
158A - Next Round
71A - Way Too Long Words
160A - Twins
1A - Theatre Square
1614B - Divan and a New Project
791A - Bear and Big Brother
1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters
1478B - Nezzar and Lucky Number
228A - Is your horseshoe on the other hoof
122A - Lucky Division
1611C - Polycarp Recovers the Permutation
432A - Choosing Teams
758A - Holiday Of Equality